package nl.arjanfrans.stopwatch;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

public class MainActivity extends Activity {
    private TextView elapsedTime_text;
    private Stopwatch stopwatch;
    private Button button_start_stop;
    private Button button_reset;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stopwatch = new Stopwatch();

        elapsedTime_text = (TextView) findViewById(R.id.elapsedTime_text);
        button_start_stop = (Button) findViewById(R.id.button_start_stop);
        button_reset = (Button) findViewById(R.id.button_reset);

        handler = new Handler();
        handler.postDelayed(update_view, 100);

    }

    private Runnable update_view = new Runnable() {
        public void run() {
            if(!stopwatch.isPaused()) {
                elapsedTime_text.setText(stopwatch.getTimeText());
                handler.postDelayed(this, 100);
            }
        }
    };

    public void onStartStopClicked(View view) {
        if(stopwatch.isPaused()) {
            button_start_stop.setText(getString(R.string.stop));
            stopwatch.start();
            handler.postDelayed(update_view, 100);
        }
        else {
            button_start_stop.setText(getString(R.string.start));
            stopwatch.pause();
        }
    }

    private void reset_alert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.reset));
        builder.setMessage(getString(R.string.reset_message));
        builder.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                reset_stopwatch();
            }
        });
        builder.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //nothing
            }
        });
        builder.show();
    }

    private void reset_stopwatch() {
        button_start_stop.setText(getString(R.string.start));
        elapsedTime_text.setText("00:00:00:000");
        stopwatch.reset();
        handler.removeCallbacks(update_view);
    }

    public void onResetClicked(View view) {
        reset_alert();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.quit));
        builder.setMessage(getString(R.string.quit_message));
        builder.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        }).setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //nothing
            }
        });
        builder.show();
    }


}
